package com.sc.nft.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sc.nft.dao.UserEcologyPointsWalletDao;
import com.sc.nft.dao.UserEcologyPointsWalletLogDao;
import com.sc.nft.dao.UserInfoDao;
import com.sc.nft.dto.UserEcologyHatchRecordsDao;
import com.sc.nft.entity.UserEcologyPointsWalletLog;
import com.sc.nft.entity.UserInfo;
import com.sc.nft.entity.vo.AdminEcologyPointsUserWalletDataVO;
import com.sc.nft.entity.vo.AdminEcologyPointsUserWalletPageDetailsVO;
import com.sc.nft.entity.vo.PointsWalletLogConsumePointsVO;
import com.sc.nft.enums.EcologyPointsEnum;
import com.sc.nft.service.UserEcologyPointsWalletLogService;
import io.swagger.annotations.ApiModelProperty;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;

/**
 * <p>
 * 用户生态积分钱包明细 服务实现类
 * </p>
 *
 * @author liGen
 * @since 2024-11-26
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class UserEcologyPointsWalletLogServiceImpl implements UserEcologyPointsWalletLogService {

    private final UserEcologyPointsWalletLogDao userEcologyPointsWalletLogDao;

    private final UserInfoDao userInfoDao;

    private final UserEcologyHatchRecordsDao userEcologyHatchRecordsDao;

    private final UserEcologyPointsWalletDao userEcologyPointsWalletDao;



    @Override
    public Boolean save(UserEcologyPointsWalletLog userEcologyPointsWalletLog) {
        return userEcologyPointsWalletLogDao.save(userEcologyPointsWalletLog);
    }

    @Override
    public Page<AdminEcologyPointsUserWalletPageDetailsVO> getUserDetailsPage(Long userId, Long walletId ,EcologyPointsEnum type, Date startTime, Date endTime, Integer pageNo, Integer pageSize) {
        Page<AdminEcologyPointsUserWalletPageDetailsVO> result = userEcologyPointsWalletLogDao.getUserDetailsPage(new Page<>(pageNo, pageSize), userId,walletId, ObjectUtil.isNull(type) ? null : type.getCode(), startTime, endTime);
        result.getRecords().forEach(r ->{
            if (r.getType() == EcologyPointsEnum.HATCH ||
                    r.getType() == EcologyPointsEnum.CAST ||
                    r.getType() == EcologyPointsEnum.GIFT_PROPS ||
                    r.getType() == EcologyPointsEnum.REFORGE){
                UserInfo userInfo = userInfoDao.getById(r.getCreateId());
                if (ObjectUtil.isNotNull(userInfo)){
                    r.setUserName(userInfo.getNickName());
                }
            }
        });
        return result;
    }


    /**
     * 数据总览 积分
     * @param pointsId
     * @param startTime
     * @param endTime
     * @return
     */

    @Override
    public AdminEcologyPointsUserWalletDataVO adminEcologyPointsUserWalletData(Long pointsId, Date startTime, Date endTime){
        if (pointsId == null) {
            return new AdminEcologyPointsUserWalletDataVO();
        }

        //用户总投入
        BigDecimal dynamicRewardAmount = userEcologyHatchRecordsDao.getDynamicRewardAmount(pointsId, null, null);
        //总发放（不包含Dao）
        //铸造消耗总积分  重铸消耗总积分

        //用户剩余总积分
        BigDecimal userBalanceAmount = userEcologyPointsWalletDao.getUserBalanceAmount(pointsId);

        //用户总投入
        BigDecimal cycleDynamicRewardAmount = userEcologyHatchRecordsDao.getDynamicRewardAmount(pointsId, startTime, endTime);

        AdminEcologyPointsUserWalletDataVO adminEcologyPointsUserWalletDataVO = new AdminEcologyPointsUserWalletDataVO();
        adminEcologyPointsUserWalletDataVO.setDynamicRewardAmount(dynamicRewardAmount);
        adminEcologyPointsUserWalletDataVO.setAmount(userEcologyPointsWalletLogDao.getConditionWithPoints(pointsId,Boolean.TRUE,null,null,null));
        adminEcologyPointsUserWalletDataVO.setCastSum(userEcologyPointsWalletLogDao.getConditionWithPoints(pointsId,Boolean.FALSE,EcologyPointsEnum.CAST,null,null));
        adminEcologyPointsUserWalletDataVO.setReforgeSum(userEcologyPointsWalletLogDao.getConditionWithPoints(pointsId,Boolean.FALSE,EcologyPointsEnum.REFORGE,null,null));
        adminEcologyPointsUserWalletDataVO.setUserBalanceAmount(userBalanceAmount);
        adminEcologyPointsUserWalletDataVO.setCycleDynamicRewardAmountCycle(cycleDynamicRewardAmount);
        adminEcologyPointsUserWalletDataVO.setCycleAmount(userEcologyPointsWalletLogDao.getConditionWithPoints(pointsId,Boolean.TRUE,null,startTime,endTime));
        adminEcologyPointsUserWalletDataVO.setCycleCastSum(userEcologyPointsWalletLogDao.getConditionWithPoints(pointsId,Boolean.FALSE,EcologyPointsEnum.CAST,startTime,endTime));
        adminEcologyPointsUserWalletDataVO.setCycleReforgeSum(userEcologyPointsWalletLogDao.getConditionWithPoints(pointsId,Boolean.FALSE,EcologyPointsEnum.REFORGE,startTime,endTime));
        return adminEcologyPointsUserWalletDataVO;
    }




}
